package org.demo.net;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import javax.swing.JFileChooser;

/**
 * 
 * @author  
 * @date    2011-1-19
 * @file    org.demo.net.NetworkPlace.java
 */
public class NetworkPlace {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		String host = "10.163.11.41";
		String user = null;
		String pass = null;
		
		NetworkPlace t = new NetworkPlace();
		// 连接
		t.connect(host, user, pass);
		
//		t.listFiles("\\\\" + host + "\\share_dir");
//		t.readFile();
		File file = t.selectRemoteFile("\\\\" + host + "\\share_dir");
		if (file != null){
			System.out.println(file.getAbsolutePath());
		}
		
		// 断开连接
		t.disconnect(host);
	}
	/**
	 * 连接到网上邻居
	 * @param host
	 * @param user
	 * @param pass
	 */
	public void connect(String host, String user, String pass){
		String info = "net use \\\\" + host;
		if (user != null && !user.equals("")){
			info += " " + pass + " /user:" + user;
		}
		try {
			Process process = Runtime.getRuntime().exec(info);
			InputStream is = process.getInputStream();
			BufferedReader buf = new BufferedReader(new InputStreamReader(is, "gbk"));
			String data = null;
			while ((data = buf.readLine()) != null){
				System.out.println(data);
			}
			buf.close();
		} catch (IOException e) {
			System.out.println("login failed: " + e.getMessage());
		}
	}
	/**
	 * 断开网上邻居连接
	 * @param host
	 */
	public void disconnect(String host){
		String info = "net use \\\\" + host + " /del";
		try {
			Process process = Runtime.getRuntime().exec(info);
			InputStream is = process.getInputStream();
			BufferedReader buf = new BufferedReader(new InputStreamReader(is, "gbk"));
			String data = null;
			while ((data = buf.readLine()) != null){
				System.out.println(data);
			}
			buf.close();
		} catch (IOException e) {
			// Ignore.
		}
	}
	/**
	 * 选择远程文件
	 * @param path
	 * @return file or null
	 */
	public File selectRemoteFile(String path){
		JFileChooser chooser = new JFileChooser();
		chooser.setCurrentDirectory(new File("\\\\10.163.11.41\\share_dir"));
		int result = chooser.showOpenDialog(null);
		File file = null;
		if (result == JFileChooser.APPROVE_OPTION){
			file = chooser.getSelectedFile();
		}
		return file;
	}
	/**
	 * 获取网上邻居中的一个目录列表
	 */
	public void listFiles(String path){
		File file = new File(path);
		File[] files = file.listFiles();
		for (File f : files){
			if (f.isDirectory()){
				System.out.println(" dir : " + f.getAbsolutePath());
			} else {
				System.out.println("file : " + f.getAbsolutePath());
			}
		}
	}
	/**
	 * 读取网上邻居中的一个文件
	 * @throws IOException 
	 * @throws UnsupportedEncodingException 
	 */
	public void readFile() throws UnsupportedEncodingException, IOException{
		String path = "\\\\10.0.113.158\\log\\jdbc.log";
		
		InputStreamReader isr = new InputStreamReader(new FileInputStream(path), "gbk");
		BufferedReader br = new BufferedReader(isr);
		String data = null;
		while ((data = br.readLine()) != null){
			System.out.println(data);
		}
		br.close();
	}

}
